package com.lingnan.eduservice.controller;


import com.lingnan.common.utils.ResultEntity;
import com.lingnan.eduservice.entity.constant.EduConstant;
import com.lingnan.eduservice.entity.vo.AdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zzd
 * @Api : 开发文档注解
 * @RestController : ResponseBody + Controller
 * @RequestMapping : 请求路径
 * @CrossOrigin : 解决跨域问题
 * @since 2021-08-07
 */
@Api(description = "管理员登录功能")
@RestController
@RequestMapping("/eduservice/login")
@CrossOrigin
public class EduLoginController {
    /**
     * 登录功能
     * @return
     */
    @PostMapping("login")
    @ApiOperation(value = "用户登录功能")
    public ResultEntity login(ModelMap modelMap, HttpSession httpSession) {
        // 通过数据库校验 TODO

        // 校验成功后Session存储用户信息 (模拟已经获取到信息 TODO)
//        AdminVO adminVO = new AdminVO();
//        adminVO.setId("1");
//        adminVO.setName("admin");
//        adminVO.setToken("admin");
//
//        List<String> rolesList = new ArrayList<>();
//        rolesList.add("roles");
//        adminVO.setRoles(rolesList);
//        adminVO.setAvatar("https://lingnanzzd.oss-cn-shenzhen.aliyuncs.com/1.jpg");
//
        // 将登录信息存储到Session域中
//        httpSession.setAttribute(EduConstant.ATTR_NAME_LOGIN_ADMINVO,adminVO);


        return ResultEntity.ok().data("token","admin");
//        return ResultEntity.ok().data(EduConstant.ATTR_NAME_LOGIN_ADMINVO,adminVO);

    }

    /**
     * 回显用户信息功能
     * @return
     */
    @GetMapping("info")
    public ResultEntity info(HttpSession httpSession) {
        // 从Session域中获取登录到信息
//        AdminVO adminVO = (AdminVO) httpSession.getAttribute(EduConstant.ATTR_NAME_LOGIN_ADMINVO);
//
//        return ResultEntity.ok().data(EduConstant.ATTR_NAME_LOGIN_ADMINVO,adminVO);


        return ResultEntity.ok().data("name","admin").data("roles","[admin]").data("avatar","https://lingnanzzd.oss-cn-shenzhen.aliyuncs.com/1.jpg");
//        return ResultEntity.ok().data("name",adminVO.getName()).data("roles",adminVO.getRoles()).data("avatar",adminVO.getAvatar());
    }
}
